package com.yuma.rabbitmq.dlxproductor.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DLXConfig {

    // 死信交换机
    @Bean
    public Exchange dlxExchange(){
        return ExchangeBuilder.directExchange("dlx_exchange").durable(true).build();
    }

    // 死信队列
    @Bean
    public Queue dlxQueue() {
        return QueueBuilder.durable("dlx_queue").build();
    }

    // 绑定
    @Bean
    public Binding dlxBinding() {
        return BindingBuilder.bind(dlxQueue()).to(dlxExchange()).with("dlx_key").noargs();
    }

    // 普通交换机
    @Bean
    public Exchange norExchange() {
        return ExchangeBuilder.directExchange("nor_exchange").durable(true).build();
    }

    @Bean
    public Queue norQueue(){
        return QueueBuilder.durable("nor_Queue")
                .deadLetterExchange("dlx_exchange")
                .deadLetterRoutingKey("dlx_key")
                .ttl(30*60*1000)  // 30分钟
                // .maxLength(5)
                .build();
    }

    @Bean
    public Binding norBinding() {
        return BindingBuilder.bind(norQueue()).to(norExchange()).with("nor_key").noargs();
    }
}
